package br.academic.zumeh.persistence;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.transaction.RollbackException;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.academic.zumeh.model.User;


public class UserDAO extends GenericDAOImpl<User>{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3041241518587851310L;
	
	public UserDAO() {
	}

	@Override
	public List<User> listaObjetos() {
		TypedQuery<User> query = entityManager.createQuery(
				"SELECT e FROM User e", User.class);
		return query.getResultList();
	}

	@Override
	public void deletaObjetos() {
		TypedQuery<User> query = entityManager.createQuery(
				"DELETE FROM User", User.class);
		query.executeUpdate();
	}

	@Override
	public User localizaObjeto(Long objId) {
		User obj = entityManager.getReference(User.class, objId);
		return obj;
	}
	
	public boolean usuarioExiste(String login, String password) {
		User user = getUser(login,password);
		return user != null;
	}
	
	
	public User getUser(String login, String password) {
		Session session = getSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("login", login));
		criteria.add(Restrictions.eq("password", password));

		User user = (User) criteria.uniqueResult();

		return user;
	}
	
	
	private boolean isUserChecked(User newUser) {
        User userByLogin = getUserByLogin(newUser.getLogin());
        if (existsLogin(newUser.getLogin()) && existPassword(newUser.getPassword())
                        && newUser.getLogin().hashCode() == userByLogin.getLogin().hashCode()) {
                return true;
        }
        return false;
        //return (existsLogin(newUser.getLogin()) && existPassword(newUser.getPassword()));
}

	private boolean existsLogin(String login) {
		// TODO Auto-generated method stub
		return false;
	}

	private boolean existPassword(String password) {
        return getUserByPass(password) != null;
}

	private Object getUserByPass(String password) {
		// TODO Auto-generated method stub
		return null;
	}

	public User getUserByLogin(String login) {
		Session session = getSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("login", login));

		User user = (User) criteria.uniqueResult();

		return user;
	}

	public void createUser(String login, String password, String firstName,
			String lastName, String birthDay, String gender, String email,
			String local, String aboutMe) throws RollbackException {
		
		User newUser = new User();
		newUser.setAboutMe(aboutMe);
		newUser.setBirthDay(birthDay);
		newUser.setEmail(email);
		newUser.setFirstName(firstName);
		newUser.setGender(gender);
		newUser.setLastName(lastName);
		newUser.setLocal(local);
		newUser.setLogin(login);
		newUser.setPassword(password);
		//newUser.setImage(image);
		
		insereObjeto(newUser);
		
		
		
	}

	public User getUserByEmail(String email) {
		Session session = getSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("email", email));

		User user = (User) criteria.uniqueResult();

		return user;
	}
	
//	 private User getUserByPass(String password) {
////         Session session = HibernateUtil.getSession();
////         HibernateUtil.beginTransaction();
//		 
//         Criteria criteria = session.createCriteria(User.class);
//         criteria.add(Restrictions.eq("password", password));
//
//         User user = (User) criteria.uniqueResult();
//        // HibernateUtil.commitTransaction();
//
//         return user;
// }
	

}
